/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.youxin.infra.kafkaUtil;

import com.youxin.infra.entity.Item;
import com.youxin.infra.util.DemoCallBack;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ExecutionException;

public class Producer extends Thread {
    private final KafkaProducer<Integer, String> producer;
    private final String topic;
    private final Boolean isAsync;
    private List<String> names;
    private Random random;

    /**
     * 构建方法，初始化生产者对象
     *
     * @param topic
     * @param isAsync
     */
    public Producer(String topic, Boolean isAsync) {
        Properties props = new Properties();
        props.put("bootstrap.servers", KafkaProperties.KAFKA_SERVER_URL + ":" + KafkaProperties.KAFKA_SERVER_PORT);
        props.put("client.id", KafkaProperties.CLIENT_ID);
        props.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        producer = new KafkaProducer<>(props);
        this.topic = topic;
        this.isAsync = isAsync;

        List<String> names = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            names.add("HAT" + i);
            names.add("TIE" + i);
            names.add("SHOE" + i);
        }
        this.names = names;
        this.random = new Random();
    }

    @Override
    public void run() {

        while (true) {
            Item item = generateItem();
            long startTime = System.currentTimeMillis();

            if (isAsync) {
                producer.send(new ProducerRecord(topic, item.toString()), new DemoCallBack(startTime, item.toString()));
                System.out.println("Sent message: (" + ", " + item.toString() + ")");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    producer.send(new ProducerRecord(topic, item)).get();
                    System.out.println("Sent message: (" + ", " + item + ")");
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private Item generateItem() {
        Item item = new Item();
        item.setTs(new Timestamp(System.currentTimeMillis()));
        item.setId(random.nextInt(100000));
        item.setName(names.get(random.nextInt(names.size())));
        item.setCounts(random.nextInt(100000));
        return item;
    }
}
